Duplicate zeros

Time: O(N); Space: O(1); easy

Given a fixed length array A of integers, duplicate each occurrence of zero, shifting the remaining elements to the right. Note that elements beyond the length of the original array are not written.

Do the above modifications to the input array in place, do not return anything from your function.

Example 1:

Input: A = [1, 0, 2, 3, 0, 4, 5, 0]

Output: None

Explanation:

  • After calling your function, the input array is modified to: [1,0,0,2,3,0,0,4]

Example 2:

Input: A = [1, 2, 3]

Output: None

Explanation:

  • After calling your function, the input array is modified to: [1,2,3]

Notes:

  • 1 <= len(A) <= 10000

  • 0 <= A[i] <= 9

[5]:
class Solution1(object):
    def duplicateZeros(self, A):
        """
        :type A: List[int]
        :rtype: None; Do not return anything, modify A in-place instead.
        """
        shift, i = 0, 0

        while i + shift < len(A):
            shift += int(A[i] == 0)
            i += 1
        i -= 1

        while shift:
            if i + shift < len(A):
                A[i + shift] = A[i]
            if A[i] == 0:
                shift -= 1
                A[i + shift] = A[i]
            i -= 1
[6]:
s = Solution1()
A = [1, 0, 2, 3, 0, 4, 5, 0]
assert s.duplicateZeros(A) == None
A = [1, 2, 3]
assert s.duplicateZeros(A) == None